<template>
  <div>
    <!-- 头部导航 -->
    <van-nav-bar placeholder :fixed="true">
      <template #left>
        <img src="../../assets/toutiao_logo.png" alt="" class="logo" />
      </template>
      <template #right>
        <van-icon name="search" size="0.48rem" color="#fff" @click="searchFn"/>
      </template>
    </van-nav-bar>
    <!-- 标签栏 -->
    <van-tabs v-model="active" animated sticky offset-top="1.2266667rem">
      <van-tab
        :title="obj.name"
        v-for="obj in userChannelList"
        :name="obj.id"
        :key="obj.id"
      >
        <!-- 文章列表组件 -->
        <article-list :channelId="active"></article-list>
      </van-tab>
    </van-tabs>
    <!-- 编辑频道图标 -->
    <van-icon
      name="plus"
      size="0.37333334rem"
      class="moreChannels"
      @click="plusFn"
    />
    <!-- 底部反馈弹框-->
    <van-action-sheet
      v-model="$store.state.homeArticleActionsShow"
      :actions="actionsArr"
      @select="onSelect"
      @cancel="cancelFn"
      @close="closeFn"
      :cancel-text="cancleText"
      get-container="body"
    />
    <!-- 编辑频道的弹出层 -->
    <van-popup v-model="show" class="popup_channel">
      <ChannelEdit
        @crossFn="crossfn"
        :userChannelList="userChannelList"
        @addChannelEvent="addChannelFn"
        @removeChannelEvevt="removeChannelFn"
        v-model="active"
      ></ChannelEdit>
    </van-popup>
  </div>
</template>

<script>
import ChannelEdit from './ChannelEdit'
import { mapState, mapMutations } from 'vuex'
import {
  getUserChannelListAPI,
  dislikeArticleAPI,
  reportArticleAPI,
  setUserChannelAPI,
  removeChannelAPI
} from '@/api'
import ArticleList from './ArticleList.vue'
import { Toast } from 'vant'

export default {

  components: { ArticleList, ChannelEdit },
  data () {
    return {
      active: 0, // 频道ID
      userChannelList: [], // 用户已选频道
      show: false,
      actionsArr: [], // 反馈内容数组
      cancleText: '取消'
    }
  },
  computed: {
    ...mapState(['firstActions', 'secondActions', 'actionsArtId'])
  },
  async created () {
    const res = await getUserChannelListAPI()
    console.log(res)
    this.userChannelList = res.data.data.channels
    this.actionsArr = this.firstActions
  },
  methods: {
    ...mapMutations(['changeHomeArticleActionsShow']),
    async onSelect (item) {
      console.log(item)
      // 判断点击的是 '反馈垃圾内容' 时切换到secondActions 二级反馈内容
      if (item.name === '反馈垃圾内容') {
        this.actionsArr = this.secondActions // 显示二级反馈
        this.cancleText = '返回'
      } else if (item.name === '不感兴趣') {
        const res = await dislikeArticleAPI(this.actionsArtId)
        console.log(res)
        this.changeHomeArticleActionsShow(false) // 关闭弹出层
        Toast.success('反馈成功')
      } else {
        // 两个一级进不去就进这里 调用接口
        const res = await reportArticleAPI({
          artId: this.actionsArtId,
          type: item.value
        })
        console.log(res)
        // 关闭弹出层
        this.changeHomeArticleActionsShow(false)
        this.cancleText = '取消'
        // 返回一级反馈页面
        setTimeout(() => {
          this.actionsArr = this.firstActions
          Toast.success('举报成功')
        }, 500)
      }

      // this.cancel-text = '返回'
      // 默认情况下点击选项时不会自动收起
      // 可以通过 close-on-click-action 属性开启自动收起
    },
    // 取消点击事件
    cancelFn () {
      // 判断点击的是'返回'
      if (this.cancleText === '返回') {
        this.changeHomeArticleActionsShow(true) // 不能让窗口关闭
        this.actionsArr = this.firstActions // 返回到一级反馈内容
        this.cancleText = '取消' // 修改'返回' 为 '取消'
      }
    },
    // 点击 + 号事件
    plusFn () {
      this.show = true
    },
    // 编辑频道 - X 点击事件
    crossfn (bool) {
      this.show = bool
    },
    // 面板关闭时 返回一级反馈面板
    closeFn () {
      setTimeout(() => {
        this.actionsArr = this.firstActions
        this.cancleText = '取消'
      }, 200)
    },
    // 添加频道
    async addChannelFn (channelObj) {
      // 先调用接口 设置用户频道
      this.userChannelList.push(channelObj)
      // 处理用户已选频道数组
      const newArr = []
      this.userChannelList.forEach((obj, index) => {
        if (obj.id === 0) {
        } else {
          // 声明一个全新对象 放入obj
          const newObj = { ...obj }
          // 设置seq属性 删除name属性 再push到newArr
          newObj.seq = index
          delete newObj.name
          newArr.push(newObj)
        }
      })

      const res = await setUserChannelAPI(newArr)
      console.log(res)
    },
    // 删除指定频道
    async removeChannelFn (channelId) {
      const res = await removeChannelAPI(channelId)
      console.log(res)
      // 删除数组里对应的频道对象
      const index = this.userChannelList.findIndex(
        obj => obj.id === channelId
      )
      this.userChannelList.splice(index, 1)
    },
    // 搜索
    searchFn () {
      this.$router.push('/search')
    }
  },

  destroyed () {
    // 销毁时触发, 页面离开, 关闭底部弹出层
    this.changeHomeArticleActionsShow(false)
  }
}
</script>

<style scoped lang='less'>
.logo {
  width: 100px;
  height: 30px;
}
// 设置 tabs 容器的样式
/deep/ .van-tabs__wrap {
  padding-right: 30px;
  background-color: #fff;
}

// 设置小图标的样式
.moreChannels {
  position: fixed;
  top: 62px;
  right: 8px;
  z-index: 999;
}
.popup_channel {
  background-color: transparent;
  height: 100%;
  width: 100%;
  background-color: #fff;
}
</style>
